/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package helper;

import com.sun.lwuit.Image;
import com.sun.lwuit.util.Resources;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.file.FileConnection;

/**
 *
 * @author TOSHIBA
 */
public class MobileDownloader {

    public MobileDownloader() {
    }

    public static byte[] download(String URL) {
        byte[] retval = null;
        HttpConnection connection = null;
        DataInputStream inputStream = null;

        try {
            connection = (HttpConnection) Connector.open(URL); //Open HTTP Connection

            System.out.println("Status line code : " + connection.getResponseCode());
            System.out.println("Status line message : " + connection.getResponseMessage());

            if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
                System.out.println(connection.getHeaderField(0) + "-" + connection.getHeaderFieldKey(0));
                System.out.println("Header Field Date : " + connection.getHeaderField("date"));
                System.out.println("Get File : " + connection.getFile());

                String str; //Printing out the downloaded bytes
                inputStream = connection.openDataInputStream();

                int length = (int) connection.getLength();

                if (length != -1) {
                    System.out.println("length = " + length);
                    retval = new byte[length];

                    inputStream.readFully(retval);

                    str = new String(retval);
                } else {
                    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
                    int ch;
                    while ((ch = inputStream.read()) != -1) {
                        bytestream.write(ch);
                    }

                    str = new String(bytestream.toByteArray());
                    bytestream.close();
                }

                //System.out.println("Content = " + str);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception error) {
                    error.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception error) {
                    error.printStackTrace();
                }
            }
        }

        return retval;
    }

    

    public static boolean saveByteToFolder(byte[] bytes, String pathAndFileName) {
        DataOutputStream dOStream = null;
        System.out.println("path to save = " + pathAndFileName);



        if (bytes != null) {
            try {
                FileConnection fc = (FileConnection) Connector.open(pathAndFileName);



                if (!fc.exists()) {
                    fc.create();
                } else {
                    fc.delete();
                    fc.create();


                }

                dOStream = fc.openDataOutputStream();
                dOStream.write(bytes);

                fc.close();


            } catch (IOException ex) {
                ex.printStackTrace();


            } finally {
                if (dOStream != null) {
                    try {
                        dOStream.close();


                    } catch (Exception error) {
                        error.printStackTrace();


                    }
                }
            }

            return true;


        } else {
            return false;
        }

    }
}
